package com.dongdongshop.controller;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.dongdongshop.common.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("alipay")
public class AliPayController {

    //支付
    @GetMapping("tradePay")
    @ResponseBody
    public String tradePay() {
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do",
                "2021000122680136","MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCA3rJfyP9yyl2AXEG44KFkMNtU0g6mwXWoy2en8zZF363tjCSyzOMs3gvJhdia+RooEYSa+W86mV1/jIwDI92Ks2kyJDjAQHCkD41LEcCFtLOS6VZUpnt9EbjVAlf4VSh9fHQylnQCPLw0f9R+oXxUf2KbZaDQ1eFLKgxF/cIoP0M7NML/p/ifCTOBZN18D7vl8RafiRjhaIOPEnk9jBPmepsk2TpWUgLAE/OZ4osG8xcqW7Gq40JJ7+R4AJtQha14CsliX1XV8i7bgUlBPAkHyXrEyxcLNnVVcVRFkrBwo6YNlhZxfVvQJESzhTI85Y0K8PXmJZE32dxFt//1k6LTAgMBAAECggEAU+MjT/DCQFH0vYrQiW1IAbVAAR3fxbMnSSJnLk/Go4Nwb7KLbQysUNeV6q/DCVZgLXcBz/PQ8N9GmrG9romZPCF/t9EWVCdea51oB1bd8Oto/ZgAx4e72PVBUP9+jju46L6r0QZZK9+nT7FqfKfYDBUD9k4YefjP+CK97I275oAXXLFU540YzsmyfFNgvAw2+y+Yns6FYVnDQFwoUx5tWSx/SkFLeVcyl8NR2r8tw+Xx5fmDEutTvieKX3VZm8tIl2vIupu+hyU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MV7AaOPmrZ+VnutIWHcfG4OAUH2DBABCk/cD9oFB6CN/frj8xAkNObOBWxsKqNHEtnGG5E02HVJCHfDQb1Dvw87EQ9hEvFHe/yq0PXySiBK4Z2cC6gqs82ymrJolRDuki00f8VNBsSWom+a5iaRnxCdbZjF+LD93v68enOIoExoREQtthJENbA99Ahea1rkbKpaauUpXZRuOihouO6VE9uxPZ07zE0YFHltR9UYt4ep4soiAirPgb2B46JlPuBljG216dxUtH0Cz1yxVjTPprD6elQIDAQAB","RSA2");
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
//异步接收地址，仅支持http/https，公网可访问
        request.setNotifyUrl("http://5f6f23ea.r3.cpolar.top/aliPayCallback/notifyUrl");
//同步跳转地址，仅支持http/https
        request.setReturnUrl("http://5f6f23ea.r3.cpolar.top/aliPayCallback/returnUrl");
/******必传参数******/
        JSONObject bizContent = new JSONObject();
//商户订单号，商家自定义，保持唯一性
        bizContent.put("out_trade_no", "20210817010101004");
//支付金额，最小值0.01元
        bizContent.put("total_amount", 0.01);
//订单标题，不可使用特殊符号
        bizContent.put("subject", "测试商品");
//电脑网站支付场景固定传值FAST_INSTANT_TRADE_PAY
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");

        /******可选参数******/
        //bizContent.put("time_expire", "2022-08-01 22:00:00");

        //// 商品明细信息，按需传入
        //JSONArray goodsDetail = new JSONArray();
        //JSONObject goods1 = new JSONObject();
        //goods1.put("goods_id", "goodsNo1");
        //goods1.put("goods_name", "子商品1");
        //goods1.put("quantity", 1);
        //goods1.put("price", 0.01);
        //goodsDetail.add(goods1);
        //bizContent.put("goods_detail", goodsDetail);

        //// 扩展信息，按需传入
        //JSONObject extendParams = new JSONObject();
        //extendParams.put("sys_service_provider_id", "2088511833207846");
        //bizContent.put("extend_params", extendParams);

        request.setBizContent(bizContent.toString());
        AlipayTradePagePayResponse response = null;
        try {
            response = alipayClient.pageExecute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return response.getBody();
    }
}
